Chapter 5. Scatter Plots

library(ggplot2)
library(gcookbook)
library(plyr)
library(gridExtra)
library(knitr)
library(hexbin)
library(MASS)

Section 5.1. Making a Basic Scatterplot

ggplot(heightweight, aes(x=ageYear, y=heightIn)) + geom_point()

five1 <- ggplot(heightweight, aes(x=ageYear, y=heightIn)) + geom_point(shape=21)

five2 <- ggplot(heightweight, aes(x=ageYear, y=heightIn)) + geom_point(size=1.5)

grid.arrange(five1, five2, ncol = 2)

Section 5.2. Grouping Data Points by a Variable Using Shape or Color

c5.21<- ggplot(heightweight, aes(x=ageYear, y=heightIn, colour=sex)) + geom_point()

c5.22 <- ggplot(heightweight, aes(x=ageYear, y=heightIn, shape=sex)) + geom_point()

grid.arrange(c5.21, c5.22, ncol= 2)

ggplot(heightweight, aes(x=ageYear, y=heightIn, shape=sex, colour=sex)) +
    geom_point() +
    scale_shape_manual(values=c(1,2)) +
    scale_colour_brewer(palette="Set1")

Section 5.3. Using Different Point Shapes

ggplot(heightweight, aes(x=ageYear, y=heightIn)) + geom_point(shape=3)

ggplot(heightweight, aes(x=ageYear, y=heightIn, shape=sex)) +
    geom_point(size=3) + scale_shape_manual(values=c(1, 4))

hw <- heightweight

hw$weightGroup <- cut(hw$weightLb, breaks=c(-Inf, 100, Inf),
                      labels=c("< 100", ">= 100"))


ggplot(hw, aes(x=ageYear, y=heightIn, shape=sex, fill=weightGroup)) +
    geom_point(size=2.5) +
    scale_shape_manual(values=c(21, 24)) +
    scale_fill_manual(values=c(NA, "black"),
      guide=guide_legend(override.aes=list(shape=21)))

Section 5.4. Mapping a Continuous Variable to Color or Size

 ggplot(heightweight, aes(x=ageYear, y=heightIn, colour=weightLb)) + geom_point()

ggplot(heightweight, aes(x=ageYear, y=heightIn, size=weightLb)) + geom_point()

ggplot(heightweight, aes(x=ageYear, y=heightIn, fill=weightLb)) +
    geom_point(shape=21, size=2.5) +
    scale_fill_gradient(low="black", high="white")

ggplot(heightweight, aes(x=ageYear, y=heightIn, fill=weightLb)) +
    geom_point(shape=21, size=2.5) +
    scale_fill_gradient(low="black", high="white", breaks=seq(70, 170, by=20),
                        guide=guide_legend())

ggplot(heightweight, aes(x=ageYear, y=heightIn, size=weightLb, colour=sex)) +
    geom_point(alpha=.5) +
    scale_size_area() +    
    scale_colour_brewer(palette="Set1")

Section 5.5 Dealing with Overplotting

sp <- ggplot(diamonds, aes(x=carat, y=price))

sp + geom_point()

sp + geom_point(alpha=.1)

sp + geom_point(alpha=.01)

sp + stat_bin2d()

sp + stat_bin2d(bins=50) +
    scale_fill_gradient(low="lightblue", high="red", limits=c(0, 6000))

sp + stat_binhex() +
    scale_fill_gradient(low="lightblue", high="red",
                        limits=c(0, 8000))

sp + stat_binhex() +
    scale_fill_gradient(low="lightblue", high="red",
                        breaks=c(0, 250, 500, 1000, 2000, 4000, 6000),
                        limits=c(0, 6000))

sp1 <- ggplot(ChickWeight, aes(x=Time, y=weight))

sp1 + geom_point()

sp1 + geom_point(position="jitter")

sp1 + geom_point(position=position_jitter(width=.5, height=0))

sp1 + geom_boxplot(aes(group=Time))

Section 5.6. Adding Fitted Regression Model Lines

sp <- ggplot(heightweight, aes(x=ageYear, y=heightIn))

sp + geom_point() + stat_smooth(method=lm)

sp + geom_point() + stat_smooth(method=lm, level=0.99)

sp + geom_point() + stat_smooth(method=lm, se=FALSE)

 sp + geom_point(colour="grey60") +
    stat_smooth(method=lm, se=FALSE, colour="black")

sp + geom_point(colour="grey60") + stat_smooth()

sp + geom_point(colour="grey60") + stat_smooth(method=loess)

b <- biopsy
b$classn[b$class=="benign"]    <- 0
b$classn[b$class=="malignant"] <- 1
ggplot(b, aes(x=V1, y=classn)) +
    geom_point(position=position_jitter(width=0.3, height=0.06), alpha=0.4,
               shape=21, size=1.5) +
  stat_smooth(method=glm, family=binomial)

sps <- ggplot(heightweight, aes(x=ageYear, y=heightIn, colour=sex)) +
       geom_point() +
       scale_colour_brewer(palette="Set1")

sps + geom_smooth()

sps + geom_smooth(method=lm, se=FALSE, fullrange=TRUE)

Section 5.7. Adding Fitted Lines from an Existing Model

model <- lm(heightIn ~ ageYear + I(ageYear^2), heightweight)
xmin <- min(heightweight$ageYear)
xmax <- max(heightweight$ageYear)
predicted <- data.frame(ageYear=seq(xmin, xmax, length.out=100))
predicted$heightIn <- predict(model, predicted)
predicted
     ageYear heightIn
1   11.58000 56.82624
2   11.63980 57.00047
3   11.69960 57.17294
4   11.75939 57.34363
5   11.81919 57.51255
6   11.87899 57.67969
7   11.93879 57.84507
8   11.99859 58.00867
9   12.05838 58.17051
10  12.11818 58.33056
11  12.17798 58.48885
12  12.23778 58.64537
13  12.29758 58.80011
14  12.35737 58.95308
15  12.41717 59.10428
16  12.47697 59.25371
17  12.53677 59.40136
18  12.59657 59.54725
19  12.65636 59.69136
20  12.71616 59.83370
21  12.77596 59.97426
22  12.83576 60.11306
23  12.89556 60.25008
24  12.95535 60.38533
25  13.01515 60.51881
26  13.07495 60.65051
27  13.13475 60.78045
28  13.19455 60.90861
29  13.25434 61.03500
30  13.31414 61.15962
31  13.37394 61.28247
32  13.43374 61.40354
33  13.49354 61.52284
34  13.55333 61.64037
35  13.61313 61.75613
36  13.67293 61.87012
37  13.73273 61.98233
38  13.79253 62.09277
39  13.85232 62.20144
40  13.91212 62.30834
41  13.97192 62.41346
42  14.03172 62.51682
43  14.09152 62.61840
44  14.15131 62.71821
45  14.21111 62.81625
46  14.27091 62.91251
47  14.33071 63.00700
48  14.39051 63.09973
49  14.45030 63.19067
50  14.51010 63.27985
51  14.56990 63.36726
52  14.62970 63.45289
53  14.68949 63.53675
54  14.74929 63.61884
55  14.80909 63.69916
56  14.86889 63.77770
57  14.92869 63.85447
58  14.98848 63.92947
59  15.04828 64.00270
60  15.10808 64.07416
61  15.16788 64.14384
62  15.22768 64.21176
63  15.28747 64.27790
64  15.34727 64.34226
65  15.40707 64.40486
66  15.46687 64.46568
67  15.52667 64.52474
68  15.58646 64.58202
69  15.64626 64.63752
70  15.70606 64.69126
71  15.76586 64.74322
72  15.82566 64.79342
73  15.88545 64.84184
74  15.94525 64.88848
75  16.00505 64.93336
76  16.06485 64.97646
77  16.12465 65.01779
78  16.18444 65.05735
79  16.24424 65.09514
80  16.30404 65.13116
81  16.36384 65.16540
82  16.42364 65.19787
83  16.48343 65.22857
84  16.54323 65.25750
85  16.60303 65.28465
86  16.66283 65.31003
87  16.72263 65.33364
88  16.78242 65.35548
89  16.84222 65.37555
90  16.90202 65.39384
91  16.96182 65.41037
92  17.02162 65.42512
93  17.08141 65.43810
94  17.14121 65.44930
95  17.20101 65.45874
96  17.26081 65.46640
97  17.32061 65.47229
98  17.38040 65.47641
99  17.44020 65.47875
100 17.50000 65.47933
sp <- ggplot(heightweight, aes(x=ageYear, y=heightIn)) +
      geom_point(colour="grey40")

sp + geom_line(data=predicted, size=1)

predictvals <- function(model, xvar, yvar, xrange=NULL, samples=100, ...) {


  if (is.null(xrange)) {
    if (any(class(model) %in% c("lm", "glm")))
      xrange <- range(model$model[[xvar]])
    else if (any(class(model) %in% "loess"))
      xrange <- range(model$x)
  }

  newdata <- data.frame(x = seq(xrange[1], xrange[2], length.out = samples))
  names(newdata) <- xvar
  newdata[[yvar]] <- predict(model, newdata = newdata, ...)
  newdata
}

modlinear <- lm(heightIn ~ ageYear, heightweight)

modloess  <- loess(heightIn ~ ageYear, heightweight)

lm_predicted    <- predictvals(modlinear, "ageYear", "heightIn")
loess_predicted <- predictvals(modloess, "ageYear", "heightIn")

sp + geom_line(data=lm_predicted, colour="red", size=.8) +
     geom_line(data=loess_predicted, colour="blue", size=.8)

library(MASS) 
b <- biopsy

b$classn[b$class=="benign"]    <- 0
b$classn[b$class=="malignant"] <- 1

fitlogistic <- glm(classn ~ V1, b, family=binomial)

glm_predicted <- predictvals(fitlogistic, "V1", "classn", type="response")

ggplot(b, aes(x=V1, y=classn)) +
    geom_point(position=position_jitter(width=.3, height=.08), alpha=0.4,
               shape=21, size=1.5) +
    geom_line(data=glm_predicted, colour="#1177FF", size=1)

Section 5.8. Adding Fitted Lines from Multiple Existing Models

make_model <- function(data) {
    lm(heightIn ~ ageYear, data)
}

models <- dlply(heightweight, "sex", .fun = make_model)

predvals <- ldply(models, .fun=predictvals, xvar="ageYear", yvar="heightIn")



ggplot(heightweight, aes(x=ageYear, y=heightIn, colour=sex)) +
    geom_point() + geom_line(data=predvals)

predvals <- ldply(models, .fun=predictvals, xvar="ageYear", yvar="heightIn",
                   xrange=range(heightweight$ageYear))


 ggplot(heightweight, aes(x=ageYear, y=heightIn, colour=sex)) +
    geom_point() + geom_line(data=predvals)

Section 5.9. Adding Annotations with Model Coefficients

model <- lm(heightIn ~ ageYear, heightweight)
summary(model)

Call:
lm(formula = heightIn ~ ageYear, data = heightweight)

Residuals:
    Min      1Q  Median      3Q     Max 
-8.3517 -1.9006  0.1378  1.9071  8.3371 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  37.4356     1.8281   20.48   <2e-16 ***
ageYear       1.7483     0.1329   13.15   <2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 2.989 on 234 degrees of freedom
Multiple R-squared:  0.4249,    Adjusted R-squared:  0.4225 
F-statistic: 172.9 on 1 and 234 DF,  p-value: < 2.2e-16
pred <- predictvals(model, "ageYear", "heightIn")
sp <- ggplot(heightweight, aes(x=ageYear, y=heightIn)) + geom_point() +
    geom_line(data=pred)

sp + annotate("text", label="r^2=0.42", x=16.5, y=52)

sp + annotate("text", label="r^2 == 0.42", parse = TRUE, x=16.5, y=52)

eqn <- as.character(as.expression(
  substitute(italic(y) == a + b * italic(x) * "," ~~ italic(r)^2 ~ "=" ~ r2,
    list(a = format(coef(model)[1], digits=3),
         b = format(coef(model)[2], digits=3),
         r2 = format(summary(model)$r.squared, digits=2)
        ))))

sp + annotate("text", label=eqn, parse=TRUE, x=Inf, y=-Inf, hjust=1.1, vjust=-.5)

Section 5.10. Adding Marginal Rugs to a Scatterplot

ggplot(faithful, aes(x=eruptions, y=waiting)) + geom_point() + geom_rug()

ggplot(faithful, aes(x=eruptions, y=waiting)) + geom_point() +
    geom_rug(position="jitter", size=.2)

Section 5.11. Labeling Point in a Scatterplot

sp <- ggplot(subset(countries, Year==2009 & healthexp>2000),
             aes(x=healthexp, y=infmortality)) +
      geom_point()

sp + annotate("text", x=4350, y=5.4, label="Canada") +
     annotate("text", x=7400, y=6.8, label="USA")

sp + geom_text(aes(label=Name), size=4)

sp + geom_text(aes(label=Name), size=4, vjust=0)

sp + geom_text(aes(y=infmortality+.1, label=Name), size=4, vjust=0)

sp + geom_text(aes(label=Name), size=4, hjust=0)

sp + geom_text(aes(x=healthexp+100, label=Name), size=4, hjust=0)

cdat <- subset(countries, Year==2009 & healthexp>2000)

cdat$Name1 <- cdat$Name

idx <- cdat$Name1 %in% c("Canada", "Ireland", "United Kingdom", "United States",
                         "New Zealand", "Iceland", "Japan", "Luxembourg",
                         "Netherlands", "Switzerland")
idx
 [1] FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE  TRUE
[12]  TRUE FALSE  TRUE  TRUE FALSE  TRUE  TRUE FALSE FALSE FALSE FALSE
[23] FALSE FALSE  TRUE  TRUE  TRUE
cdat$Name1[!idx] <- NA

ggplot(cdat, aes(x=healthexp, y=infmortality)) +
    geom_point() +
    geom_text(aes(x=healthexp+100, label=Name1), size=4, hjust=0) +
    xlim(2000, 10000)

Section 5.12. Creating a Balloon Plot

cdat <- subset(countries, Year==2009 &
    Name %in% c("Canada", "Ireland", "United Kingdom", "United States",
                "New Zealand", "Iceland", "Japan", "Luxembourg",
                "Netherlands", "Switzerland"))

p <- ggplot(cdat, aes(x=healthexp, y=infmortality, size=GDP)) +
     geom_point(shape=21, colour="black", fill="cornsilk")


p

p + scale_size_area(max_size=15)

hec <- HairEyeColor[,,"Male"] + HairEyeColor[,,"Female"]

library(reshape2)
hec <- melt(hec, value.name="count")

ggplot(hec, aes(x=Eye, y=Hair)) +
    geom_point(aes(size=count), shape=21, colour="black", fill="cornsilk") +
    scale_size_area(max_size=20, guide=FALSE) +
    geom_text(aes(y=as.numeric(Hair)-sqrt(count)/22, label=count), vjust=1,
              colour="grey60", size=4)

Section 5.13. Making a Scatterplot Matrix

c2009 <- subset(countries, Year==2009,
                select=c(Name, GDP, laborrate, healthexp, infmortality))

c2009
                                Name         GDP laborrate  healthexp
50                       Afghanistan          NA      59.8   50.88597
101                          Albania   3772.6047      59.5  264.60406
152                          Algeria   4022.1989      58.5  267.94653
203                   American Samoa          NA        NA         NA
254                          Andorra          NA        NA 3089.63589
305                           Angola   4068.5757      81.3  203.80787
356              Antigua and Barbuda  12501.8666        NA  652.53169
407                        Argentina   7665.0734      65.0  730.17301
458                          Armenia   2768.6126      66.3  128.85761
509                            Aruba          NA        NA         NA
560                        Australia  42130.8203      65.2 3867.42853
611                          Austria  45555.4345      60.4 5037.31089
662                       Azerbaijan   4808.1688      63.0  284.72528
713                     Bahamas, The  20916.2842      73.3 1557.65512
764                          Bahrain  17608.8298      63.8 1107.90631
815                       Bangladesh    607.7649      70.7   18.43125
866                         Barbados  13181.3416      71.7 1041.44095
917                          Belarus   5182.6304      60.2  294.61477
968                          Belgium  43640.1962      53.5 5104.01899
1019                          Belize   4056.1224      64.1  217.15214
1070                           Benin    771.7088      72.7   31.92885
1121                         Bermuda  88746.8944        NA         NA
1172                          Bhutan   1772.2838      62.7   97.98912
1223                         Bolivia   1774.1952      71.9   84.78763
1274          Bosnia and Herzegovina   4523.3114      61.2  494.87951
1325                        Botswana   5790.1819      76.6  611.92342
1376                          Brazil   8251.0616      70.7  734.05138
1427               Brunei Darussalam  27390.0500      67.5  790.72871
1478                        Bulgaria   6403.1477      54.5  474.84637
1529                    Burkina Faso    509.2978      84.4   38.07546
1580                         Burundi    162.8704      89.3   19.78891
1631                        Cambodia    748.1512      79.3   42.10335
1682                        Cameroon   1157.0245      67.0   61.05385
1733                          Canada  39599.0418      67.8 4379.76084
1784                      Cape Verde   3227.9520      66.4  146.10513
1835                  Cayman Islands          NA        NA         NA
1886        Central African Republic    458.5672      79.0   19.33792
1937                            Chad    625.3019      70.4   41.78443
1988                 Channel Islands          NA        NA         NA
2039                           Chile   9487.0111      57.3  787.19332
2090                           China   3748.9344      73.7  177.14653
2141                        Colombia   5165.7319      58.6  323.01081
2192                         Comoros    747.9125      79.6   27.83652
2243                Congo, Dem. Rep.    174.5076      70.8   15.58425
2294                     Congo, Rep.   2430.5255      72.7   70.07210
2345                      Costa Rica   6369.1663      62.7  668.49256
2396                   Cote d'Ivoire   1190.7895      66.9   55.25489
2447                         Croatia  14322.6081      53.0 1120.37109
2498                            Cuba          NA      53.9  707.39670
2549                         Curacao          NA        NA         NA
2600                          Cyprus  31298.4375      62.3         NA
2651                  Czech Republic  18136.8451      57.9 1383.65144
2702                         Denmark  55933.3545      65.4 6272.72868
2753                        Djibouti   1202.9199      70.1   84.45174
2804                        Dominica   5532.0537        NA  361.29188
2855              Dominican Republic   4756.3591      65.1  270.64760
2906                         Ecuador   3647.6990      62.3  255.49979
2957                Egypt, Arab Rep.   2370.7111      48.8  113.29717
3008                     El Salvador   3425.1706      59.9  228.57189
3059               Equatorial Guinea  17944.4045      65.5  709.40713
3110                         Eritrea    364.2048      72.6   10.12132
3161                         Estonia  14238.8817      61.2 1004.42550
3212                        Ethiopia    393.6832      85.4   14.68076
3263                  Faeroe Islands  45205.9305        NA         NA
3314                            Fiji   3314.2707      58.7  130.41002
3365                         Finland  44576.7270      60.9 4309.60396
3416                          France  40663.0502      56.1 4797.96556
3467                French Polynesia          NA      57.3         NA
3518                           Gabon   7411.1839      75.5  266.26454
3569                     Gambia, The    436.1472      77.8   25.62880
3620                         Georgia   2441.0105      63.7  255.62737
3671                         Germany  40658.5823      59.8 4628.76920
3722                           Ghana   1098.4257      74.6   45.05245
3773                       Gibraltar          NA        NA         NA
3824                          Greece  28936.4809      53.7 3040.73383
3875                       Greenland  22507.8887        NA         NA
3926                         Grenada   5906.4568        NA  446.52663
3977                            Guam          NA      64.4         NA
4028                       Guatemala   2684.9664      66.9  186.12313
4079                          Guinea    426.6530      84.2   18.77023
4130                   Guinea-Bissau    562.4150      71.5   18.35889
4181                          Guyana   2689.8559      63.5  132.50867
4232                           Haiti    656.7792      69.9   39.60249
4283                        Honduras   1921.8795      59.9  117.06353
4334            Hong Kong SAR, China  29881.8144      60.0         NA
4385                         Hungary  12847.3031      50.1  937.98617
4436                         Iceland  37972.2370      77.5 3130.39086
4487                           India   1195.0003      57.6   44.80258
4538                       Indonesia   2271.7753      68.9   55.44365
4589              Iran, Islamic Rep.   4525.9486      52.8  268.75470
4640                            Iraq   2096.8511      41.3   98.45406
4691                         Ireland  49737.9274      63.6 4951.84469
4742                     Isle of Man          NA        NA         NA
4793                          Israel  26102.3506      57.1 1966.47189
4844                           Italy  35073.3225      49.1 3327.62987
4895                         Jamaica   4693.4275      64.7  231.08176
4946                           Japan  39456.4388      59.5 3321.46639
4997                          Jordan   4242.1537      49.3  335.81544
5048                      Kazakhstan   7240.5703      70.6  330.11134
5099                           Kenya    744.4031      82.2   33.24912
5150                        Kiribati   1305.8038        NA  158.50643
5201                Korea, Dem. Rep.          NA      66.0         NA
5252                     Korea, Rep.  17109.9851      60.9 1107.94833
5303                          Kosovo   3010.9934        NA         NA
5354                          Kuwait  41364.6893      68.5 1416.09990
5405                 Kyrgyz Republic    881.3605      66.6   57.09366
5456                         Lao PDR    997.2401      78.3   35.82477
5507                          Latvia  11475.6923      61.5  750.44926
5558                         Lebanon   8321.3707      46.1  663.27358
5609                         Lesotho    800.4202      74.0   70.04993
5660                         Liberia    229.2703      71.1   29.35613
5711                           Libya   9957.4904      52.8  416.74524
5762                   Liechtenstein 134914.6728        NA         NA
5813                       Lithuania  11033.5885      55.7  729.78492
5864                      Luxembourg 106252.2442      55.5 8182.85511
5915                Macao SAR, China  40919.3262      69.4         NA
5966                  Macedonia, FYR   4510.2380      54.0  313.68971
6017                      Madagascar    421.7802      86.4   17.97023
6068                          Malawi    327.3363      76.8   19.06556
6119                        Malaysia   6908.6611      62.0  336.43858
6170                        Maldives   4230.0510      67.1  330.69865
6221                            Mali    601.2609      51.9   38.42789
6272                           Malta  19326.4588      49.4 1446.43275
6323                Marshall Islands   2861.6376        NA  422.49589
6374                      Mauritania    896.1959      70.0   21.91725
6425                       Mauritius   6951.2787      57.5  383.05315
6476                         Mayotte          NA        NA         NA
6527                          Mexico   7879.6773      61.4  514.80196
6578           Micronesia, Fed. Sts.   2498.2833        NA  336.91627
6629                         Moldova   1525.5315      49.6  180.89118
6680                          Monaco 172676.3407        NA 7137.38972
6731                        Mongolia   1690.4170      72.9   74.19826
6782                      Montenegro   6569.0869        NA  616.83557
6833                         Morocco   2842.3235      52.3  155.67512
6884                      Mozambique    428.1975      85.8   24.72483
6935                         Myanmar          NA      73.8   12.47223
6986                         Namibia   4095.5044      57.1  257.96755
7037                           Nepal    438.1784      71.5   25.34454
7088                     Netherlands  48068.3540      66.1 5163.74038
7139                   New Caledonia          NA      57.0         NA
7190                     New Zealand  29352.4540      68.6 2633.62461
7241                       Nicaragua   1088.1658      62.4  104.69487
7292                           Niger    351.2742      62.7   20.88920
7343                         Nigeria   1091.1344      56.2   69.29737
7394        Northern Mariana Islands          NA        NA         NA
7445                          Norway  78408.7138      66.9 7661.60977
7496                            Oman  17280.0972      55.7  496.64996
7547                        Pakistan    950.1192      54.3   22.55684
7598                           Palau   8094.5632        NA  980.54244
7649                          Panama   6955.7448      64.6  590.72387
7700                Papua New Guinea   1180.6904      72.9   36.69268
7751                        Paraguay   2245.3284      71.9  158.85965
7802                            Peru   4412.3903      67.1  200.78948
7853                     Philippines   1835.6365      63.8   66.88271
7904                          Poland  11287.7389      53.7  803.64614
7955                        Portugal  22029.8691      62.5 2409.66063
8006                     Puerto Rico          NA      46.1         NA
8057                           Qatar  61531.6921      84.3 1714.99341
8108                         Romania   7500.3404      52.4  408.32754
8159              Russian Federation   8614.6729      62.8  475.24572
8210                          Rwanda    510.3116      86.0   48.18416
8261                           Samoa   2721.9439      57.5  205.00032
8312                      San Marino          NA        NA 4089.27133
8363           Sao Tome and Principe   1168.8878      59.8   90.73473
8414                    Saudi Arabia  13900.6307      54.5  713.85023
8465                         Senegal   1056.4957      76.4   58.90160
8516                          Serbia   5689.8052        NA  419.03919
8567                      Seychelles   9027.7301        NA  365.72463
8618                    Sierra Leone    323.4532      66.4   43.85885
8669                       Singapore  36757.5243      64.7 1501.33140
8720       Sint Maarten (Dutch part)          NA        NA         NA
8771                 Slovak Republic  16174.2284      59.5 1372.68818
8822                        Slovenia  24101.2632      58.9 2174.71765
8873                 Solomon Islands   1147.2437      37.5   71.91242
8924                         Somalia          NA      70.3         NA
8975                    South Africa   5733.0410      55.0  485.43365
9026                     South Sudan          NA        NA         NA
9077                           Spain  31891.3861      58.6 3075.01325
9128                       Sri Lanka   2035.3030      54.2   83.61577
9179             St. Kitts and Nevis  10168.0093        NA  633.91741
9230                       St. Lucia   5544.8437      63.0  442.57331
9281        St. Martin (French part)          NA        NA         NA
9332  St. Vincent and the Grenadines   5357.2512      67.5  301.33061
9383                           Sudan   1286.1473      52.3   94.59385
9434                        Suriname   6255.2800      52.2  429.12629
9485                       Swaziland   2512.9783      63.6  155.78038
9536                          Sweden  43406.1748      64.8 4251.97636
9587                     Switzerland  63524.6523      66.9 7140.72895
9638            Syrian Arab Republic   2691.5977      50.4   72.00606
9689                      Tajikistan    733.8741      67.0   37.99971
9740                        Tanzania    502.8518      88.4   25.31169
9791                        Thailand   3838.2272      72.9  167.70008
9842                     Timor-Leste    543.6922      71.0   73.24377
9893                            Togo    534.8508      74.4   28.93053
9944                           Tonga   3149.9735      64.6  161.18296
9995             Trinidad and Tobago  14684.0532      66.1 1069.21297
10046                        Tunisia   4168.9509      48.0  239.96116
10097                         Turkey   8553.7415      46.8  570.97184
10148                   Turkmenistan   3710.4536      68.0   77.06955
10199       Turks and Caicos Islands          NA        NA         NA
10250                         Tuvalu   2609.9266        NA  290.02138
10301                         Uganda    488.2459      84.5   42.54540
10352                        Ukraine   2545.4803      58.1  179.56664
10403           United Arab Emirates  33183.1701      77.6 1520.05855
10454                 United Kingdom  35163.4149      62.2 3285.05021
10505                  United States  45744.5596      65.0 7410.16301
10556                        Uruguay   9364.1241      64.1  698.16329
10607                     Uzbekistan   1181.8601      64.6   62.15114
10658                        Vanuatu   2635.3138      83.9  105.83174
10709                  Venezuela, RB  11490.0291      66.0  686.34793
10760                        Vietnam   1129.2889      71.9   79.71064
10811          Virgin Islands (U.S.)          NA      59.5         NA
10862             West Bank and Gaza          NA      42.8         NA
10913                    Yemen, Rep.   1130.1833      46.8   64.00204
10964                         Zambia   1006.3882      69.2   47.05637
11015                       Zimbabwe    467.8534      66.8         NA
      infmortality
50           103.2
101           17.2
152           32.0
203             NA
254            3.1
305           99.9
356            7.3
407           12.7
458           18.4
509             NA
560            4.2
611            3.6
662           41.1
713           14.0
764            8.9
815           40.0
866           17.1
917            4.5
968            3.6
1019          14.9
1070          74.7
1121            NA
1172          45.6
1223          43.4
1274           7.5
1325          37.1
1376          18.4
1427           5.9
1478          11.1
1529          93.3
1580          88.5
1631          45.6
1682          85.2
1733           5.2
1784          29.9
1835            NA
1886         106.8
1937          99.6
1988            NA
2039           7.7
2090          16.8
2141          17.1
2192          64.0
2243         112.8
2294          60.9
2345           8.8
2396          87.3
2447           4.9
2498           4.7
2549            NA
2600           3.3
2651           3.3
2702           3.4
2753          74.1
2804          11.5
2855          23.2
2906          18.2
2957          20.0
3008          15.0
3059          82.4
3110          43.7
3161           4.8
3212          69.5
3263            NA
3314          15.4
3365           2.5
3416           3.5
3467            NA
3518          55.2
3569          57.8
3620          20.5
3671           3.5
3722          51.3
3773            NA
3824           3.5
3875            NA
3926           9.4
3977            NA
4028          25.9
4079          83.6
4130          93.1
4181          26.2
4232          59.3
4283          21.2
4334            NA
4385           5.7
4436           1.7
4487          49.5
4538          28.1
4589          22.8
4640          31.9
4691           3.4
4742            NA
4793           3.7
4844           3.2
4895          20.7
4946           2.4
4997          18.9
5048          29.9
5099          56.3
5150          39.6
5201          26.4
5252           4.3
5303            NA
5354           9.6
5405          34.0
5456          44.3
5507           8.5
5558          19.4
5609          67.0
5660          77.6
5711          14.0
5762           1.8
5813           5.7
5864           2.2
5915            NA
5966          10.6
6017          44.9
6068          61.4
6119           5.6
6170          15.0
6221         100.6
6272           5.2
6323          22.8
6374          75.4
6425          13.2
6476            NA
6527          14.9
6578          34.9
6629          16.8
6680           3.4
6731          27.8
6782           7.5
6833          31.7
6884          94.6
6935          52.2
6986          31.6
7037          43.3
7088           3.8
7139            NA
7190           4.9
7241          23.7
7292          74.7
7343          90.4
7394            NA
7445           2.9
7496           8.4
7547          70.7
7598          15.1
7649          17.5
7700          47.7
7751          21.4
7802          16.1
7853          23.9
7904           5.4
7955           3.2
8006            NA
8057           7.0
8108          12.4
8159           9.8
8210          62.7
8261          17.5
8312           1.9
8363          53.6
8414          15.5
8465          50.8
8516           6.5
8567          11.7
8618         116.5
8669           2.1
8720            NA
8771           7.0
8822           2.5
8873          23.0
8924         108.3
8975          42.5
9026            NA
9077           4.0
9128          14.7
9179           7.0
9230          14.0
9281            NA
9332          19.2
9383          66.9
9434          27.3
9485          57.4
9536           2.4
9587           4.1
9638          14.3
9689          54.2
9740          52.5
9791          11.6
9842          49.2
9893          67.1
9944          13.8
9995          24.4
10046         14.8
10097         15.0
10148         48.0
10199           NA
10250         27.6
10301         65.0
10352         11.7
10403          6.4
10454          4.7
10505          6.6
10556          9.7
10607         44.5
10658         12.5
10709         16.1
10760         19.3
10811           NA
10862         20.7
10913         58.7
10964         71.5
11015         52.2
panel.cor <- function(x, y, digits=2, prefix="", cex.cor, ...) {
    usr <- par("usr")
    on.exit(par(usr))
    par(usr = c(0, 1, 0, 1))
    r <- abs(cor(x, y, use="complete.obs"))
    txt <- format(c(r, 0.123456789), digits=digits)[1]
    txt <- paste(prefix, txt, sep="")
    if(missing(cex.cor)) cex.cor <- 0.8/strwidth(txt)
    text(0.5, 0.5, txt, cex =  cex.cor * (1 + r) / 2)
}


panel.hist <- function(x, ...) {
    usr <- par("usr")
    on.exit(par(usr))
    par(usr = c(usr[1:2], 0, 1.5) )
    h <- hist(x, plot = FALSE)
    breaks <- h$breaks
    nB <- length(breaks)
    y <- h$counts
    y <- y/max(y)
    rect(breaks[-nB], 0, breaks[-1], y, col="white", ...)
}


pairs(c2009[,2:5], upper.panel = panel.cor,
                   diag.panel  = panel.hist,
                   lower.panel = panel.smooth)

panel.lm <- function (x, y, col = par("col"), bg = NA, pch = par("pch"),
                            cex = 1, col.smooth = "black", ...) {
    points(x, y, pch = pch, col = col, bg = bg, cex = cex)
    abline(stats::lm(y ~ x),  col = col.smooth, ...)
}



 pairs(c2009[,2:5], pch=".",
                   upper.panel = panel.cor,
                   diag.panel  = panel.hist,
                   lower.panel = panel.lm)


END!